package com.jz.community.modulemine.ui.activity;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.ActivityUtil;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.commview.view.widget.LoginButton;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.bean.UserChangeOtherBean;
import com.jz.community.modulemine.presenter.changePasswordPresenter;
import com.jz.community.modulemine.ui.changePasswordView;
import com.jz.community.modulemine.ui.view.WaitingDialog;

import butterknife.BindView;

/**
 * Created by Admin on 2018/4/4 0004.
 */

public class ChangePasswordActivity extends BaseMvpActivity<changePasswordView.View, changePasswordPresenter> implements changePasswordView.View {

    @BindView(R2.id.title_back)
    ImageButton titleBack;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.edt_old_password)
    EditText edtOldPassword;
    @BindView(R2.id.edt_new_password)
    EditText edtNewPassword;
    @BindView(R2.id.edt_password)
    EditText edtPassword;
    @BindView(R2.id.button_submit)
    LoginButton buttonSubmit;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;

    WaitingDialog waitDialog;


    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected int getContentView() {
        return R.layout.module_mine_activity_change_password;
    }

    @Override
    protected changePasswordPresenter createPresenter() {
        return new changePasswordPresenter(this);
    }

    @Override
    protected void initView() {
        setImmersionBar(titleToolbar);
        titleName.setText("修改密码");
        buttonSubmit.setText(this.getString(R.string.confirm));
        waitDialog = new WaitingDialog(this);
    }

    @Override
    protected void addListener() {
        titleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        buttonSubmit.setOnClickListener(v -> submit());
    }

    private void submit() {
        String oldPwd = edtOldPassword.getText().toString();
        String pwd = edtNewPassword.getText().toString();
        String pwd1 = edtPassword.getText().toString();
        if (isHasEmptyDta(oldPwd, pwd, pwd1)) {
           // buttonSubmit.startAnim();
            mPresenter.changePassword("", oldPwd, pwd1);
            waitDialog.show();
        }
    }

    public boolean isHasEmptyDta(String oldPwd, String pwd, String pwd1) {
        if (Preconditions.isNullOrEmpty(pwd)) {
            WpToast.l(getContext(), "新密码不能为空");
            return false;
        } else {
            if (pwd.length() < 6) {
                WpToast.l(getContext(), "新密码不能少于6位");
                return false;
            }
        }
        if (Preconditions.isNullOrEmpty(pwd1)) {
            WpToast.l(getContext(), "确认密码不能为空");
            return false;
        } else {
            if (!pwd.equals(pwd1)) {
                WpToast.l(getContext(), "两次输入的密码不相同");
                return false;
            }
        }
        return true;
    }

    @Override
    public void changePasswordResult(UserChangeOtherBean responseText) {
        waitDialog.dismiss();
        if (responseText != null) {
            WpToast.l(getContext(), "修改成功");
            ActivityUtil.finishActivity(SettingActivity.class);
            ActivityUtil.finishActivity(ChangePasswordActivity.class);
            ARouter.getInstance().build(RouterIntentConstant.MODULE_AUTHORIZATION_LOGIN).navigation();
        }
    }

    @Override
    public void showError(String s, int code) {
        waitDialog.dismiss();
        if (!Preconditions.isNullOrEmpty(s)) {
            WpToast.l(getContext(), s);
        }
       // refreshRegainBackground();
    }

    /**
     * 刷新按钮
     */
    private void refreshRegainBackground() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                buttonSubmit.regainBackground(ChangePasswordActivity.this.getString(R.string.confirm));
            }
        }, 1000);
    }
}
